#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define y1 y_alpha_1

using namespace std;

using ll = long long;
using db = double;
using vec = vector<int>;
using pii = pair<int,int>;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-',ch = getchar();
	while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
	return f ? -x : x;
}

const int N = 1e5 + 10;

int ch[N][2],siz[N],si[N],fa[N],lz[N],n,m,stk[N],top;

void pushup(int x) {siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + si[x] + 1;}
void pushdn(int x) {if(lz[x]) lz[ch[x][0]] ^= 1,lz[ch[x][1]] ^= 1,swap(ch[x][0],ch[x][1]),lz[x] = 0;}
bool isroot(int x) {return ch[fa[x]][0] != x && ch[fa[x]][1] != x;}
void Rotate(int x) {
	int y = fa[x],z = fa[y],k = ch[y][1] == x;
	if(!isroot(y)) ch[z][ch[z][1] == y] = x; fa[x] = z;
	if(ch[x][k ^ 1]) fa[ch[x][k ^ 1]] = y; ch[y][k] = ch[x][k ^ 1];
	ch[x][k ^ 1] = y; fa[y] = x; pushup(y); pushup(x);
}
void Splay(int x) {
	stk[top = 1] = x; for(int t = x; !isroot(t) ; t = fa[t]) stk[++top] = fa[t]; while(top) pushdn(stk[top--]);
	while(!isroot(x)) {
		int y = fa[x] , z = fa[y];
		if(!isroot(y)) (ch[y][0] == x) ^ (ch[z][0] == y) ? Rotate(x) : Rotate(y);
		Rotate(x);
	}
}
void access(int x) {for(int y = 0; x ; y = x , x = fa[x]) Splay(x),si[x] += siz[ch[x][1]] , si[x] -= siz[ch[x][1] = y],pushup(x);}
void makeroot(int x) {access(x); Splay(x); lz[x] ^= 1;}
void link(int x,int y) {makeroot(x); access(y); Splay(y); fa[x] = y; si[y] += siz[x]; pushup(y);}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in; m = in; for(int i = 1;i <= n;i++) siz[i] = 1;
	while(m--) {
		char op = getchar(); while(!isalpha(op)) op = getchar();
		int x = in,y = in;
		if(op == 'A') link(x,y);
		else makeroot(x),access(y),Splay(y),printf("%lld\n",(ll) (si[x] + 1) * (si[y] + 1));
	}
	return 0;
}
